package com.study.readwrite;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author wanghao
 * @version v1.0
 * @description: 读写锁降级
 * @date 2021/12/28 18:06
 * @change [修改时间] [修改者]@[版本号] [修改内容说明]
 */
public class Demo {

    public static void main(String[] args) {
        // 可重入读写锁对象
        ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
        // 读锁
        ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();
        // 写锁
        ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();

        // 锁降级
        // 获取读锁
        writeLock.lock();
        System.out.println("---获取写锁");

        readLock.lock();
        System.out.println("----获取读锁");

        writeLock.unlock();
        readLock.unlock();

    }
}
